<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\HomeAccessLog;
use app\common\model\HomeAccessConfig;
use app\common\model\HomeBwList;

	/**
	 * 后台管理前台访问控制器
	 * @author Yel 
	 * @copyright  yel
	 */

class HomeAccess extends AdminBase{

	//+------------------------------------
	//|   前台用户访问数据 start  2019/7/8
	//+-------------------------------------

	/**
	 * 访问列表
	 * @author Yel 
	 * @copyright  yel 2019/7/8-9
	 * @param  string  $ip  ip
	 * @param  string  $start  开始时间
	 * @param  string  $end    结束时间
	 */
	public function lst(){
		//搜索
		$where = [];
		$ip = input('param.ip','');
		$starttime = input('param.start','');
		$endtime   = input('param.end','');
		if($starttime){
			$where['addtime'] = ['egt',strtotime($starttime)];
		}
		if($endtime){
			$where['addtime'] = ['elt',strtotime($endtime)];
		}
		if($endtime && $starttime){
			$where['addtime'] = [['egt',strtotime($starttime)],['elt',strtotime($endtime)],'and'];
		}
		if(!empty($ip)){
			$where['ip'] = ['like','%'.$ip.'%'];
		}
		//实例化
		$HomeAccessLog = new HomeAccessLog();
		//查询日志
		$data = $HomeAccessLog->pageData($where,20,false,'*','addtime desc');
		//日志处理
		foreach ($data['data'] as $key => $value) {
			$data['data'][$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
		}
		//渲染
		$this->assign('data',$data['data']);
		$this->assign('page',$data['page']);
		return view();
	}


	/**
	 * 删除访问记录，支持批量删除
	 * @author Yel 
	 * @copyright  yel 2019/7/9
	 * @param string id  访问记录ID
	 */
	public function del(){
		//获取参数
		$id = input('param.id','');
		//普通验证参数
		if(empty($id)){
			$this->result($id,-1,'参数错误:参数为null','json');
		}

		//实例化
		$User = new HomeAccessLog();
		//删除操作
		$op = $User -> deleteData($id);
		if($op){
			$this->result($op,200,'操作成功','json');
		}
		$this->result($id,-1,'该操作失败：数据连接错误','json');
	}


	/**
	 * 清空所有访问记录
	 * @author Yel 
	 * @copyright  yel 2019/7/9
	 */
	public function delAll(){
		//实例化
		$User = new HomeAccessLog();
		//删除操作
		$op = $User -> deleteData(['id' =>['gt',0]]);
		if($op){
			$this->result($op,200,'操作成功','json');
		}
		$this->result($id,-1,'该操作失败：数据连接错误','json');
	}

	/**
	 * 访问次数//分组查询
	 * @author Yel 
	 * @copyright  yel 2019/7/8-9
	 * @param  string  $ip  ip
	 * @param  string  $start  开始时间
	 * @param  string  $end    结束时间
	 */
	public function number_of_list(){
		//搜索
		$where = [];
		$ip = input('param.ip','');
		$starttime = input('param.start','');
		$endtime   = input('param.end','');
		$sort = input('param.sort',1);
		$order = 'number desc';
		if($sort != 1){
			$order = 'number asc';
		}
		if($starttime){
			$where['addtime'] = ['egt',strtotime($starttime)];
		}
		if($endtime){
			$where['addtime'] = ['elt',strtotime($endtime)];
		}
		if($endtime && $starttime){
			$where['addtime'] = [['egt',strtotime($starttime)],['elt',strtotime($endtime)],'and'];
		}
		if(!empty($ip)){
			$where['ip'] = ['like','%'.$ip.'%'];
		}
		//实例化
		$HomeAccessLog = new HomeAccessLog();
		//查询日志
		$data = $HomeAccessLog->pageData($where,20,false,'*,count(*) as number',$order,'ip');
		//日志处理
		foreach ($data['data'] as $key => $value) {
			$data['data'][$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
		}
		//渲染
		$this->assign('data',$data['data']);
		$this->assign('page',$data['page']);
		return view('number_of_list');
	}

	/**
	 * 分组删除
	 * @author Yel 
	 * @copyright  yel 2019/7/9
	 * @param string ip  访问记录Ip
	 */
	public function dels(){
		//获取参数
		$ip = input('param.ip','');
		//普通验证参数
		if(empty($ip)){
			$this->result($ip,-1,'参数错误:参数为null','json');
		}

		//实例化
		$User = new HomeAccessLog();
		//删除操作
		$op = $User -> deleteData(['ip'=>['in',$ip]]);
		if($op){
			$this->result($op,200,'操作成功','json');
		}
		$this->result($id,-1,'该操作失败：数据连接错误','json');
	}

	//+--------------------------------
	//|   前台用户访问数据 end
	//+--------------------------------





	//+--------------------------------
	//|    黑白名单列表 start
	//+--------------------------------
	/**
	 * 黑名单列表
	 * @author Yel 
	 * @copyright  yel 2019/7/12
	 */
	public function black_lst(){
		//搜索
		$where = [];
		$ip = input('param.ip','');
		$starttime = input('param.start','');
		$endtime   = input('param.end','');
		if($starttime){
			$where['addtime'] = ['egt',strtotime($starttime)];
		}
		if($endtime){
			$where['addtime'] = ['elt',strtotime($endtime)];
		}
		if($endtime && $starttime){
			$where['addtime'] = [['egt',strtotime($starttime)],['elt',strtotime($endtime)],'and'];
		}
		if(!empty($ip)){
			$where['ip'] = ['like','%'.$ip.'%'];
		}
		//实例化
		$where['type'] = 0;
		$HomeBwList = new HomeBwList();
		//查询黑名单列表
		$data = $HomeBwList->pageData($where,20);
		//数据处理
		foreach ($data['data'] as $key => $value) {
			$data['data'][$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
		}
		$this->assign('data',$data['data']);
		$this->assign('page',$data['page']);
		return view('black_lst');
	}


	/**
	 * 白名单列表
	 * @author Yel 
	 * @copyright  yel 2019/7/12
	 */
	public function white_lst(){
		//搜索
		$where = [];
		$ip = input('param.ip','');
		$starttime = input('param.start','');
		$endtime   = input('param.end','');
		if($starttime){
			$where['addtime'] = ['egt',strtotime($starttime)];
		}
		if($endtime){
			$where['addtime'] = ['elt',strtotime($endtime)];
		}
		if($endtime && $starttime){
			$where['addtime'] = [['egt',strtotime($starttime)],['elt',strtotime($endtime)],'and'];
		}
		if(!empty($ip)){
			$where['ip'] = ['like','%'.$ip.'%'];
		}
		//实例化
		$where['type'] = 1;
		$HomeBwList = new HomeBwList();
		//查询黑名单列表
		$data = $HomeBwList->pageData($where,20);
		//数据处理
		foreach ($data['data'] as $key => $value) {
			$data['data'][$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
		}
		$this->assign('data',$data['data']);
		$this->assign('page',$data['page']);
		return view('white_lst');	
	}

	/**
	 * 添加黑白名单
	 * @author Yel 
	 * @copyright  yel 2019/7/12
	 * @param  string  ip      ip地址
	 * @param  int     type    类型：0：黑名单   1：白名单
	 * @param  string  desc    备注
	 */
	public function bw_add(){
		if(request()->isPost()){
			//获取数据
			$data['ip']   = input('param.ip');
			$data['type'] = input('param.type');
			$data['desc'] = input('param.desc');
			$data['addtime'] = time(); 

			//添加数据
			$HomeBwList = new HomeBwList();
		    $op = $HomeBwList->addData($data);
		    if($op){
		    	$this->result($op,200,'添加成功','json');
		    }
		    $this->result($op,-1,'添加失败','json');
		}
		$this->assign('type',input('param.type'));
		return view('bw_add');
	}

	/**
	 * 删除黑白名单
	 * @author Yel 
	 * @copyright  yel 2019/7/12
	 * @param  int  id  
	 */
	public function bw_del(){
		//获取参数
		$id = input('param.id','');
		//普通验证参数
		if(empty($id)){
			$this->result($id,-1,'参数错误:参数为null','json');
		}
		//实例化
		$HomeBwList = new HomeBwList();
		//删除操作
		$op = $HomeBwList -> deleteData($id);
		if($op){
			$this->result($op,200,'操作成功','json');
		}
		$this->result($id,-1,'该操作失败：数据连接错误','json');
	}

	/**
	 * 修改黑白名单
	 * @author Yel 
	 * @copyright  yel 2019/7/12
	 */
	public function bw_edit(){
		$HomeBwList = new HomeBwList();

		if(request()->isPost()){
			//获取数据
			$id           = input('param.id');
			$data['ip']   = input('param.ip');
			$data['type'] = input('param.type');
			$data['desc'] = input('param.desc');

			//修改数据
		    $op = $HomeBwList->saveData($data,['id'=>$id]);
		    if($op){
		    	$this->result($op,200,'修改成功','json');
		    }
		    $this->result($op,-1,'修改失败','json');
		}
		$op = input('param.id');
		$result = $HomeBwList->findData($op);
		$this->assign('data',$result);
		return view('bw_edit');
	}

	//+--------------------------------
	//|    黑白名单列表 end
	//+--------------------------------

	//+--------------------------------
	//|    访问配置 start
	//+--------------------------------
	/**
	 * 访问配置数据
	 * @author Yel 
	 * @copyright  yel 2019/7/12
	 */
	public function access_config_lst(){
		//找出配置
		$HomeAccessConfig = new HomeAccessConfig();
		$access_config = $HomeAccessConfig->selectData(['key'=>['in','QIANTAI,HOUTAI']]);
		//开辟空间
		$data = [];
		//对配置进行重组
		foreach ($access_config as $key => $value) {
			if($value['key'] === 'QIANTAI'){
				//前台访问检测
				$data['status1'] =  $value['status'];
				$data['flag1']   =  $value['flag'];
				$data['id1']   =  $value['id'];
			}
			if($value['key'] === 'HOUTAI'){
				//后台访问检测
				$data['status2'] =  $value['status'];
				$data['flag2']   =  $value['flag'];
				$data['id2']   =  $value['id'];
			}
		}

		//渲染
		$this->assign('data',$data);
		return view('access_config_lst');
	}

	/**
	 * 修改访问配置数据
	 * @author Yel 
	 * @copyright  yel 2019/7/12
	 */
	public function access_config_edit(){
		//接受数据
		$data[0]['id'] = input('post.id1');
		$data[0]['status'] = input('post.status1');
		$data[0]['flag'] = input('post.flag1');

		$data[1]['id'] = input('post.id2');
		$data[1]['status'] = input('post.status2');
		$data[1]['flag'] = input('post.flag2');

		//实例化模型
		$HomeAccessConfig = new HomeAccessConfig();

		$op = $HomeAccessConfig->saveAllData($data);
		if($op){
			$this->result($op,200,'保存成功','json');
		}
		$this->result($op,-1,'保存失败','json');
	}
	//+--------------------------------
	//|    访问配置 end
	//+--------------------------------

}